<template>
  <div class="app-container">
    <el-form
        :model="queryParams"
        size="small"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="120"
    >
      <el-form-item label="联系电话" prop="phone">
        <el-input
            v-model="queryParams.phone"
            placeholder="请输入联系电话"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基础疾病" prop="jcjb">
        <el-select v-model="queryParams.jcjb" placeholder="基础疾病" clearable>
          <el-option
              v-for="dict in ky_jcjb"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="吸烟史" prop="xys">
        <el-select v-model="queryParams.xys" placeholder="吸烟史" clearable>
          <el-option
              v-for="dict in ky_xys"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="饮酒情况" prop="yjqk">
        <el-select v-model="queryParams.yjqk" placeholder="饮酒情况" clearable>
          <el-option
              v-for="dict in ky_yjqk"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="饮食情况" prop="hrjgl">
        <el-select
            v-model="queryParams.hrjgl"
            placeholder="红肉或加工类饮食"
            clearable
        >
          <el-option
              v-for="dict in ky_hrjgl"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="有无一级亲属患有结直肠癌" prop="jzcaqsYw">
        <el-select
            v-model="queryParams.jzcaqsYw"
            placeholder="有无一级亲属患有结直肠癌"
            clearable
        >
          <el-option
              v-for="dict in ky_yw"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否做肠镜" prop="cjjcSf">
        <el-select
            v-model="queryParams.cjjcSf"
            placeholder="近期是否做过肠镜"
            clearable
        >
          <el-option
              v-for="dict in ky_sf"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="肠镜结果" prop="cj">
        <el-select
            v-model="queryParams.cj"
            placeholder="肠镜检查结果"
            clearable
        >
          <el-option
              v-for="dict in ky_jcjg"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="Septin9检查结果" prop="s9">
        <el-select v-model="queryParams.s9" placeholder="Septin9检查结果" clearable>
          <el-option
              v-for="dict in ky_yyxjg"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="FIT_DNA结果" prop="fitDna">
        <el-select
            v-model="queryParams.fitDna"
            placeholder="FIT_DNA结果"
            clearable
        >
          <el-option
              v-for="dict in ky_yyxjg"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="CEA检查结果" prop="cea">
        <el-select v-model="queryParams.cea" placeholder="CEA检查结果" clearable>
          <el-option
              v-for="dict in ky_jcjg"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="CA19-9检查结果" prop="ca199">
        <el-select v-model="queryParams.ca199" placeholder="CA19-9检查结果" clearable>
          <el-option
              v-for="dict in ky_jcjg"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="12">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['keyan:patient:add']"
        >新增
        </el-button
        >

        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['keyan:patient:export']"
        >导出
        </el-button
        >
      </el-col>

      <el-col :span="12" style="display: flex; flex-direction: row-reverse;">
        <el-button type="primary" icon="Search" @click="handleQuery" style="margin-left: 20px">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-col>
      <!-- <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table
        v-loading="loading"
        :data="patientList"
        @selection-change="handleSelectionChange"
        size="small"
    >
      <el-table-column type="selection" width="40" align="center" />
      <el-table-column label="参与者ID" align="center" prop="id" />
      <el-table-column label="联系电话" align="center" prop="phone" width="120"/>
      <el-table-column label="年龄(岁)" align="center" prop="age" />
      <el-table-column label="身高(cm)" align="center" prop="height" />
      <el-table-column label="体重(kg)" align="center" prop="weight" />
      <el-table-column label="基础疾病" align="center" prop="jcjb">
        <template #default="scope">
          <dict-tag :options="ky_jcjb" :value="scope.row.jcjb"/>
        </template>
      </el-table-column>
      <el-table-column label="吸烟史" align="center" prop="xys">
        <template #default="scope">
          <dict-tag :options="ky_xys" :value="scope.row.xys"/>
        </template>
      </el-table-column>
      <el-table-column label="饮酒情况" align="center" prop="yjqk">
        <template #default="scope">
          <dict-tag :options="ky_yjqk" :value="scope.row.yjqk"/>
        </template>
      </el-table-column>
      <el-table-column label="饮食情况" align="center" prop="hrjgl">
        <template #default="scope">
          <dict-tag :options="ky_hrjgl" :value="scope.row.hrjgl"/>
        </template>
      </el-table-column>
      <el-table-column
          label="有无一级亲属患有结直肠癌"
          align="center"
          prop="jzcaqsYw"
      >
        <template #default="scope">
          <dict-tag :options="ky_yw" :value="scope.row.jzcaqsYw"/>
        </template>
      </el-table-column>
      <el-table-column label="参与者亲属关系" align="center" prop="jzcaqsGx"/>
      <el-table-column label="近期是否做过肠镜" align="center" prop="cjjcSf">
        <template #default="scope">
          <dict-tag :options="ky_sf" :value="scope.row.cjjcSf"/>
        </template>
      </el-table-column>
      <el-table-column label="肠镜检查时间" align="center" prop="cjjcSj" />
      <el-table-column label="肠镜检查结果" align="center" prop="cjjcJg" width="120" show-overflow-tooltip>
        <template #default="scope">
          <dict-tag :options="ky_jcjg" :value="scope.row.cjjcJg"/>
        </template>
      </el-table-column>
      <el-table-column label="Septin9检查结果" align="center" prop="s9">
        <template #default="scope">
          <dict-tag :options="ky_yyxjg" :value="scope.row.s9"/>
        </template>
      </el-table-column>
      <el-table-column label="FIT_DNA结果" align="center" prop="fitDna">
        <template #default="scope">
          <dict-tag :options="ky_yyxjg" :value="scope.row.fitDna"/>
        </template>
      </el-table-column>
      <el-table-column label="CEA检查结果" align="center" prop="cea">
        <template #default="scope">
          <dict-tag :options="ky_jcjg" :value="scope.row.cea"/>
        </template>
      </el-table-column>
      <el-table-column label="CEA结果" align="center" prop="ceaJg" width="120" show-overflow-tooltip/>
      <el-table-column label="CA19-9检查结果" align="center" prop="ca199">
        <template #default="scope">
          <dict-tag :options="ky_jcjg" :value="scope.row.ca199"/>
        </template>
      </el-table-column>
      <el-table-column label="CA19-9检查结果" align="center" prop="ca199Jg" width="120" show-overflow-tooltip/>
      <!--      <el-table-column label="肠镜" align="center" prop="cj">-->
      <!--        <template #default="scope">-->
      <!--          <dict-tag :options="ky_jcjg" :value="scope.row.cj"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="其它肿瘤标志物" align="center" prop="qtzlbzw" width="120" show-overflow-tooltip/>
      <el-table-column
          label="结直肠癌高危因素量化问卷"
          align="center"
          prop="wj1Fs"
      >
        <template #default="scope">
          <div>{{ scope.row.wj1Jg }}({{ scope.row.wj1Fs }}分)</div>
        </template>
      </el-table-column>
      <el-table-column label="机会性筛查风险问卷" align="center" prop="wj2Fs">
        <template #default="scope">
          <div>{{ scope.row.wj2Jg }}({{ scope.row.wj2Fs }}分)</div>
        </template>
      </el-table-column>
      <el-table-column label="亚太结直肠癌筛查评分" align="center" prop="wj3Fs">
        <template #default="scope">
          <div>{{ scope.row.wj3Jg }}({{ scope.row.wj3Fs }}分)</div>
        </template>
      </el-table-column>
      <el-table-column label="治疗建议" align="center" prop="cjJg" width="120" show-overflow-tooltip/>
      <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          width="250"
      >
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['keyan:patient:edit']">编辑信息
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['keyan:patient:remove']">删除
          </el-button>
          <el-button link type="primary" icon="Phone" @click="handleOpenFollowup(scope.row)">回访</el-button>
        </template>
      </el-table-column>
    </el-table>

    <Pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />


    <el-dialog
        v-model="dialogDoctorListOpen"
        title="选择接诊医生"
        width="500"
        :close-on-click-modal="false"
    >
      <el-form :model="form">
        <el-form-item label="接诊医生" :label-width="120">
          <el-select v-model="selectedDoctorInfo" placeholder="选择接诊医生">
            <el-option
                v-for="item in doctorList"
                :key="item.id"
                :label="item.doctorName"
                :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogDoctorListOpen = false">取消</el-button>
          <el-button type="primary" @click="gotoStep"> 确定</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="titleFollowup" v-model="openFollowup" width="800px" style="min-height: 300px;padding: 30px"
               append-to-body>
      <el-form ref="followupRef" :model="followupForm" :rules="followupRules" label-width="160px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="回访时间" prop="followupTime">
              <el-date-picker clearable
                              v-model="followupForm.followupTime"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择回访时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="是否患结直肠癌" prop="sfZjca">
              <el-select v-model="followupForm.sfZjca" placeholder="请选择是否患结直肠癌">
                <el-option
                    v-for="dict in ky_sf"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="14">
            <el-form-item label="结直肠癌确诊时间" prop="zjcaDate">
              <el-date-picker clearable
                              v-model="followupForm.zjcaDate"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择结直肠癌确诊时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">

            <el-form-item label="是否患有其它癌症" prop="sfQtaz">
              <el-select v-model="followupForm.sfQtaz" placeholder="请选择是否患有其它癌症">
                <el-option
                    v-for="dict in ky_sf"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="14">

            <el-form-item label="其它癌症名称" prop="azmc">
              <el-input v-model="followupForm.azmc" placeholder="请输入其它癌症名称"/>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleAddFollowup">确 定</el-button>
          <el-button @click="closeFollowup">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Patient">
import {delPatient, getDoctorList, listPatient,} from "@/api/keyan/patient";
import {useRouter} from "vue-router";
import {addFollowup} from "@/api/keyan/followup.js";

const { proxy } = getCurrentInstance();
const {ky_yw, ky_jcjb, ky_hrjgl, ky_sf, ky_yjqk, ky_yyxjg, ky_xys, ky_jcjg} =
    proxy.useDict(
        "ky_yw",
        "ky_jcjb",
        "ky_hrjgl",
        "ky_sf",
        "ky_yjqk",
        "ky_yyxjg",
        "ky_xys",
        "ky_jcjg"
    );
const patientList = ref([]);
const doctorList = ref([]);
const open = ref(false);
const openFollowup = ref(false);
const loading = ref(true);
const dialogDoctorListOpen = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const titleFollowup = ref("");
const selectedDoctorInfo = ref(null);

const data = reactive({
  form: {},
  followupForm: {
    patientId: null,
    followupTime: '',
    sfZjca: '',
    zjcaDate: '',
    sfQtaz: '',
    azmc: '',
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    phone: null,
    age: null,
    height: null,
    weight: null,
    jcjb: null,
    xys: null,
    yjqk: null,
    hrjgl: null,
    jzcaqsYw: null,
    cjjcSf: null,
    cjjcJg: null,
    s9: null,
    fitDna: null,
    cea: null,
    ca199: null,
    cj: null,
    wj1Jg: null,
    wj2Jg: null,
    wj3Jg: null,
  },
  rules: {},
  followupRules: {
    followupTime: [
      {required: true, message: "回访时间不能为空", trigger: "blur"}
    ],
    sfZjca: [
      {required: true, message: "是否患结直肠癌，0：未患，1：患有不能为空", trigger: "change"}
    ],
    sfQtaz: [
      {required: true, message: "是否患有其它癌症，0否，1有不能为空", trigger: "change"}
    ],
  }
});

const router = useRouter();
const {queryParams, form, rules, followupForm, followupRules} = toRefs(data);

/** 查询参与者基础信息列表 */
function getList() {
  loading.value = true;
  listPatient(queryParams.value).then((response) => {
    patientList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function resetAddFollowupForm() {
  followupForm.value = {
    patientId: null,
    followupTime: '',
    sfZjca: '',
    zjcaDate: '',
    sfQtaz: '',
    azmc: '',
  };
  proxy.resetForm("followupRef");
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  // router.push("/keyan/patientInfo");
  // reset();
  getDoctorList().then((response) => {
    if (response.code === 200) {
      doctorList.value = response.data;
      dialogDoctorListOpen.value = true;
    } else {
      proxy.$modal.msgError("医生列表获取失败！" + response.msg);
    }
  });

}

/** 修改按钮操作 */
function handleUpdate(row) {
  router.push({
    path: "/keyan/patientInfo",
    query: {patientId: row.id},
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除参与者基础信息编号为"' + _ids + '"的数据项？')
      .then(function () {
        return delPatient(_ids);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {
      });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
      "keyan/patient/export",
      {
        ...queryParams.value,
      },
      `patient_${new Date().getTime()}.xlsx`
  );
}

function handleOpenFollowup(row) {
  openFollowup.value = true;
  resetAddFollowupForm();
  followupForm.value.patientId = row.id;
  titleFollowup.value = "为参与者【" + row.patientName + "  " + row.phone + "】增加回访记录";
}

// 取消按钮
function closeFollowup() {
  openFollowup.value = false;

}

function handleAddFollowup() {
  proxy.$refs["followupRef"].validate(valid => {
    if (valid) {
      addFollowup(followupForm.value).then(response => {
        proxy.$modal.msgSuccess("新增成功");
        closeFollowup();
      });

    }
  });
}

const gotoStep = () => {
  // console.log('选择的医生：', selectedDoctorInfo.value,doctorList.value);
  let doctorInfo = {};
  for (const item of doctorList.value) {
    if (item.id == selectedDoctorInfo.value) {
      doctorInfo = item;
      break;
    }
  }
  if (doctorInfo.id) {
    router.push({
      path: "/keyan/step",
      query: doctorInfo,
    });
    dialogDoctorListOpen.value = false;
  } else {
    proxy.$modal.msgError("请选择医生！");
  }
};

getList();
</script>

<style scoped lang="scss">
.el-form-item {
  width: 320px;
}
</style>
